import 'package:flutter/material.dart';
import 'package:city_pickers/city_pickers.dart';
import 'package:contact_picker/contact_picker.dart';
import 'dart:convert';
import '../model/cartmodellist.dart';
import 'package:toast/toast.dart';
class Addreslist extends StatefulWidget {
  Addreslist({Key key}) : super(key: key);

  @override
  _AddreslistState createState() => _AddreslistState();
}

class _AddreslistState extends State<Addreslist> {
  //提示组件
   _toast() {
    Toast.show("请完善收件人信息", context,
        duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
  }
  //焦点
  FocusNode focusNode = FocusNode();
  @override
void initState() {
  super.initState();
 
   //添加listener监听
   //对应的TextField失去或者获取焦点都会回调此监听
    focusNode.addListener((){
      if (focusNode.hasFocus) {
        print('得到焦点');
     
      }else{
      print('失去焦点');
      }
    });

}

  //通讯录
   final ContactPicker _contactPicker = new ContactPicker();
  Contact _contact;
   String v;
  //输入框
   TabController _tabController;
   var _username=new TextEditingController(); //收货人
   TextEditingController _phone = TextEditingController();
  //var _phone=new TextEditingController(); //收货人电话号
  var _city=new TextEditingController(); //收货人详细地址
  var phones;
  //区域选择
  Result resultArr = new Result();
  String adds;
  void _clickCity() async {
    Result result2 = await CityPickers.showFullPageCityPicker(
      context: context,
    );
   setState(() {
      adds=result2.provinceName+result2.cityName+result2.areaName;
   });
    print(result2);
  }
  bool val =false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: new IconButton(
               icon: new Icon(Icons.arrow_back, color: Colors.black),
               onPressed: () => Navigator.of(context).pop(),
              ),
        textTheme: TextTheme(
          
        ),
        title: Container(
         // color: Colors.white,
            child: Center(
          child: Text('添加收货地址'),
        )),
        actions: <Widget>[
          Container(
            padding: EdgeInsets.only(right:5),
            child: InkWell(
            onTap: (){
              var username=_username.text;
              var phone=_phone.text;
              var addsl=adds;
              var city=_city.text;
              var flag=val;
              print('${_username.text} ${_phone.text} ${adds} ${_city.text}');
             // Addressl().getAddress();
             if(username!=null&&phone!=null&&addsl!=null&&city!=null){
                Addressl().addresslist(username,phone,addsl,city,flag);
               // Navigator.pushNamed(context, '/address');
                Navigator.of(context).pushReplacementNamed('/address');
             }else{
               this._toast();
             }
            },
            child: Text('保存'),
          ),
          )
        ],
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            //收货人
            Container(
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(width:1,color:Colors.black38)
                )
              ),
              padding: EdgeInsets.all(2),
              child: TextField(
                decoration: InputDecoration(
                  hintText: "收货人",
                   focusedBorder:   InputBorder.none,
                   disabledBorder: InputBorder.none,
                    enabledBorder:  InputBorder.none,
                  suffixIcon: IconButton(
                    icon: Icon(Icons.contact_phone),
                    onPressed: ()async{
                       Contact contact = await _contactPicker.selectContact();
                       print(contact.phoneNumber.number);
                  setState(() {
                   _contact = contact;
                  final result =_contact.fullName ;
                final  phones=_contact.phoneNumber.number;
                   _username.text=result  ;
                 this._phone.text=phones;
                 
                  });
                    },
                  ),
                  
                ),
               // controller: _username,
               controller: TextEditingController.fromValue(TextEditingValue(
                  // 设置内容
                  text: _username.text,
                  // 保持光标在最后
                  selection: TextSelection.fromPosition(TextPosition(
                      affinity: TextAffinity.downstream,
                      offset: _username.text.length)))
                      ),
                onChanged: (value){
                  this._username.text=value;
                },
              ),
            ),
            //电话号码
             Container(
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(width:1,color:Colors.black38)
                )
              ),
              padding: EdgeInsets.all(2),
              child: TextField(
                decoration: InputDecoration(
                  hintText: "电话号码",
                   focusedBorder:   InputBorder.none,
                   disabledBorder: InputBorder.none,
                    enabledBorder:  InputBorder.none,  
                ),
               // controller: _phone,
                controller: TextEditingController.fromValue(TextEditingValue(
                  // 设置内容
                  text: _phone.text,
                  // 保持光标在最后
                  selection: TextSelection.fromPosition(TextPosition(
                      affinity: TextAffinity.downstream,
                      offset: _phone.text.length)))),
                onChanged: (value){
                setState(() {
                    this._phone.text=value;
                });
                },
              ),
            ),
            //所在区域
            Container(
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(width:1,color:Colors.black38)
                )
              ),
              padding: EdgeInsets.all(2),
              child: InkWell(
                onTap: (){
                  _clickCity();
                },
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text('${adds==null?"所在区域":adds}'),
                  IconButton(icon: Icon(Icons.chevron_right), onPressed: null),
                ],
              ),
              )
            ),
            //详细地址
             Container(
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(width:1,color:Colors.black38)
                )
              ),
              padding: EdgeInsets.all(2),
              child: TextField(
                decoration: InputDecoration(
                  hintText: "详细地址",
                   focusedBorder:   InputBorder.none,
                   disabledBorder: InputBorder.none,
                    enabledBorder:  InputBorder.none,  
                ),
               // controller: _city,
                  controller: TextEditingController.fromValue(TextEditingValue(
                  // 设置内容
                  text: _city.text,
                  // 保持光标在最后
                  selection: TextSelection.fromPosition(TextPosition(
                      affinity: TextAffinity.downstream,
                      offset: _city.text.length)))),
                focusNode: focusNode,
                 autocorrect: true,
                 maxLines: null,
                  keyboardType: TextInputType.multiline,
                onChanged: (value){
                  setState(() {
                    this._city.text=value;
                  });
                },
              ),
            ),
            //默认
            Container(
              child:  Row(
                mainAxisAlignment: MainAxisAlignment.center,
               children: <Widget>[
                 Text('默认地址'),
                  Checkbox(value:val, activeColor: Colors.pink, onChanged: (bool va){
              setState(() {
                  this.val=va;
                });
               }),
               ], 
              )
            )
          ],
        ),
      ),
    );
  }
}

